/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.MailInDTO;
import UTIL.SqlDataAccessHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author NxSiHMr0D
 */
public class MainInDAO {

    Connection _newConnect;

    /**
     * set mail to folder mail.
     * @param IDMail
     * @param IDFolder
     * @throws java.sql.SQLException
     */
    public void move2Folder(int IDMail, int IDFolder) throws SQLException {
        if (_newConnect == null || _newConnect.isClosed()) {
            _newConnect = SqlDataAccessHelper.getConnection();
        }
        PreparedStatement uprs =
                _newConnect.prepareStatement("UPDATE Mail SET MailIn=? WHERE IDMail=?");
        uprs.setInt(1, IDFolder);
        uprs.setInt(2, IDMail);
        int st = uprs.executeUpdate();
    }

    /**
     * get all of folder mails.
     * @return
     * @throws java.sql.SQLException
     */
    public ArrayList<MailInDTO> getListBox() throws SQLException {
        ArrayList<MailInDTO> lstMailIn = new ArrayList<MailInDTO>();
        if (_newConnect == null || _newConnect.isClosed()) {
            _newConnect = SqlDataAccessHelper.getConnection();
        }
        Statement uprs = (Statement) _newConnect.createStatement();
        ResultSet st = uprs.executeQuery("SELECT * FROM MailIn");
        while (st.next()) {
            MailInDTO mail = new MailInDTO();
            int id = st.getInt("IDType");
            mail.setIdMainIn(id);
            String s = st.getString("MailIn");
            mail.setNameIn(s);
            lstMailIn.add(mail);
        }
        return lstMailIn;

    }

    /**
     * insert folder to database.
     * @param folder
     * @return
     * @throws java.sql.SQLException
     */
    public int insertFolder(String folder) throws SQLException {
        if (_newConnect == null || _newConnect.isClosed()) {
            _newConnect = SqlDataAccessHelper.getConnection();
        }
        Statement stmt = (Statement) _newConnect.createStatement();
        String sqlQuery = "select max(IDType)as ID from MailIn";
        ResultSet newCode = stmt.executeQuery(sqlQuery);
        newCode.next();

        int newID = newCode.getInt(1) + 1;
        PreparedStatement uprs =
                _newConnect.prepareStatement("INSERT INTO MailIn(IDType,MailIn) VALUES(?,?)");

        uprs.setString(2, folder);
        uprs.setInt(1, newID);
        //uprs.setInt(8, accountInsert.getId());

        uprs.execute();
        return 1;

    }

    /**
     * delete folder from database.
     * @param mailIN
     * @throws java.sql.SQLException
     */
    public void deleteFolder(MailInDTO mailIN) throws SQLException {
        if (_newConnect == null || _newConnect.isClosed()) {
            _newConnect = SqlDataAccessHelper.getConnection();
        }
        PreparedStatement uprs =
                _newConnect.prepareStatement("DELETE FROM MailIn WHERE IDType = ?");
        uprs.setInt(1, mailIN.getIdMainIn());
        uprs.executeUpdate();
    }

    /**
     * search folder mail by name
     * @param name
     * @return
     * @throws java.sql.SQLException
     */
    public MailInDTO searchMailIn(String name) throws SQLException {
        if (_newConnect == null || _newConnect.isClosed()) {
            _newConnect = SqlDataAccessHelper.getConnection();
        }
        PreparedStatement uprs = _newConnect.prepareStatement("SELECT * FROM MailIn WHERE MailIn = ?");
        uprs.setString(1, name);
        ResultSet st = uprs.executeQuery();
        MailInDTO result = new MailInDTO();
        while (st.next()) {
            result.setIdMainIn(st.getInt("IDType"));
            result.setNameIn(st.getString("MailIn"));
            return result;
        }
        return null;
    }

    /**
     * search folder mail by id.
     * @param ID
     * @return
     * @throws java.sql.SQLException
     */
    public MailInDTO searchMailIn(int ID) throws SQLException {
        if (_newConnect == null || _newConnect.isClosed()) {
            _newConnect = SqlDataAccessHelper.getConnection();
        }
        PreparedStatement uprs = _newConnect.prepareStatement("SELECT * FROM MailIn WHERE IDType = ?");
        uprs.setInt(1, ID);
        ResultSet st = uprs.executeQuery();
        MailInDTO result = new MailInDTO();
        while (st.next()) {
            result.setIdMainIn(st.getInt("IDType"));
            result.setNameIn(st.getString("MailIn"));
            return result;
        }
        return null;
    }

    /**
     * modify information folder mail.
     * @param modify
     * @throws java.sql.SQLException
     */
    public void modifyFolder(MailInDTO modify) throws SQLException {
        if (_newConnect == null || _newConnect.isClosed()) {
            _newConnect = SqlDataAccessHelper.getConnection();
        }

        PreparedStatement uprs =
                _newConnect.prepareStatement("UPDATE MailIn SET MailIn = ? " +
                " WHERE IDType = ?");
        uprs.setString(1, modify.getNameIn());
        uprs.setInt(2, modify.getIdMainIn());
        int st = uprs.executeUpdate();
    }
}
